// 例子16

{
    function Point(x = 0, y = 0) {
		this.x = x;
		this.y = y;
    }

    const p = new Point(3);
    console.log(p); // Point {x:3, y:0}
}

console.log('----------------------------------');

{
    // 不可以在函数体内再对同名变量使用let或const声明
    function foo(x = 5) {
        let x = 1; // Uncaught SyntaxError: Identifier 'x' has already been declared
        const x = 2; // Uncaught SyntaxError: Identifier 'x' has already been declared
    }

    try {
        foo();
    } catch {
        console.error('Uncaught SyntaxError: Identifier \'x\' has already been declared')
        console.error('Uncaught SyntaxError: Identifier \'x\' has already been declared')
    }

}

console.log('----------------------------------');

{
    // 设置参数默认值后，函数的length属性失真
    function fn1(x, y, z){};
    function fn2(x = 0, y, z){};
    function fn3(x, y = 0, z){};
    function fn4(x, y, z = 0){};
    function fn5(x, y, ...z){};

    console.log(fn1.length, fn2.length, fn3.length, fn4.length, fn5.length); // 3 0 1 2 2
}

console.log('----------------------------------');

{
    // 设置默认值时，形参初始化时形成独立作用域的影响
    let x = 1; // 如果没有这句的话，调用fn会报错：Uncaught ReferenceError: x is not defined
    function f(y = x) {
        let x = 2;
        console.log(y);
    }

    f(); // 1


    let foo = 'outer';
    function bar(func = () => foo) {
        let foo = 'inner';
        console.log(func());
    }

    bar(); // outer
}